package com.lab.webSocket.repository;

import com.lab.webSocket.dto.UserInfoDto;
import org.hibernate.query.NativeQuery;
import org.hibernate.transform.Transformers;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;

/**
 * Created by xu_jiaHui on 2017/9/7.
 */
public class FriendListRepositoryImpl {
    @PersistenceContext
    private EntityManager em;

    public List<UserInfoDto> getFriendListByUserAccount(String userAccount) {
        String sql = "SELECT u.userAccount, userNickname, userIcon from tb_user u where userAccount in" +
                " ( SELECT FriendAccount from tb_friendsList f where f.userAccount=? union " +
                " SELECT userAccount  from tb_friendsList f where f.FriendAccount=? )";
        Query query = em.createNativeQuery(sql).setParameter(1, userAccount).setParameter(2, userAccount);
        query.unwrap(NativeQuery.class).setResultTransformer(Transformers.aliasToBean(UserInfoDto.class));
        return query.getResultList();
    }

}
